--------------------------------------- Start of Script -------------------------------------------
USE [master]
GO

-----------------------------------------------------
-- Create the database if it doesn't already exist --
-----------------------------------------------------
/****** Object:  Database [MediaLibraryDB]    Script Date: 05/27/2014 1:15:13 PM ******/
IF EXISTS (SELECT name FROM [dbo].sysdatabases WHERE name = N'MediaLibraryDB')
    DROP DATABASE [MediaLibraryDB]
GO

CREATE DATABASE [MediaLibraryDB]
GO

IF (FULLTEXTSERVICEPROPERTY('IsFullTextInstalled') = 1)
    BEGIN
        EXEC [MediaLibraryDB].[dbo].[sp_fulltext_database] @action = 'enable'
    END
GO

IF NOT EXISTS (SELECT loginname FROM [dbo].syslogins WHERE name = 'mlmadmin')
    CREATE LOGIN [mlmadmin] WITH PASSWORD=N'P@ssword1',
    DEFAULT_DATABASE=[MediaLibraryDB],
    CHECK_EXPIRATION=OFF, 
    CHECK_POLICY=OFF
GO

USE [MediaLibraryDB]
GO

IF NOT EXISTS(SELECT name FROM master.[sys].database_principals WHERE name = 'mlmadmin')
    CREATE USER [mlmadmin] FOR LOGIN [mlmadmin]
    WITH DEFAULT_SCHEMA=[dbo]
GO

EXEC sp_addrolemember [db_datawriter], [mlmadmin]
GO

EXEC sp_addrolemember [db_datareader], [mlmadmin]
GO

EXEC sp_addrolemember [db_ddladmin], [mlmadmin]
GO

EXEC sp_addrolemember [db_owner], [mlmadmin]
GO

GRANT execute to [mlmadmin]
GO
--------------------------------------- End of Script -------------------------------------------